package cn.csxw.openchina.ui.fragment.syntheticalFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.util.ArrayList;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;
import cn.csxw.openchina.dagger2.component.DaggerHotComponent;
import cn.csxw.openchina.dagger2.module.HotModule;
import cn.csxw.openchina.dagger2.presenter.HotPresenter;
import cn.csxw.openchina.entity.bean.NewsBean;
import cn.csxw.openchina.ui.adapter.HotAdapter;
import cn.csxw.openchina.utils.Global;

/**
 * 综合Fragment下的 热点Fragment
 */

public class HotFragment extends BaseRefreshFragment {
    private int pageIndex = 0;
    @Inject
    public HotPresenter mHotPresenter;
    public HotAdapter mHotAdapter;

    @Override
    protected BaseRecAdapter getAdapter() {
        if(mHotAdapter==null){
            mHotAdapter = new HotAdapter();
        }
        return mHotAdapter;
    }

    @Override
    public void doRefresh() {
        mHotAdapter.getData().clear();
        mHotPresenter.callNetWork(pageIndex,4,"week",20);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setOnRefreshListener(mOnRefreshListener);
        DaggerHotComponent.builder().hotModule(new HotModule(this)).build().inject(this);
        Global.onNetworkStateListener(mHotPresenter,this);
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mHotPresenter.callNetWork(pageIndex,4,"week",20);
    }

    public void setHotAdapterData(ArrayList<NewsBean> data) {
        mHotAdapter.setData(data);
    }

    BaseRefreshFragment.OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onUpLoadMore() {
            if (Global.isNetworkReachable(getActivity())) {
                pageIndex++;
                mHotPresenter.callNetWork(pageIndex,1,"day",20);
            }else {
                Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
            }
        }
    };
}
